package su.order.module;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import su.order.dto.OrderDto;
import su.order.entity.OrderEntity;
import su.order.entity.OrderProductEntity;
import su.order.service.OrderService;
import su.tool.SuIntUtils;
import su.user.entity.UserYwy;
import su.user.util.YwyCache;

@Controller
@RequestMapping("/admin/order")
public class OrderAdminServlet {
	
	@Autowired
	private OrderService orderService;

	@Autowired
	private YwyCache ywyCache;

	@RequestMapping("index")
	public String index(HttpServletRequest request,@ModelAttribute("_page") OrderDto orderDto, Model model) throws Exception {
		orderDto.setQueryStr(request.getQueryString() == null ? "" : request.getQueryString());
		List<Map<String,Object>> orders = orderService.queryOrderByPage(orderDto ,model);
		model.addAttribute("list", orders);
		return "/maintain/order/order_index";
	}
	
	@RequestMapping("show")
	public String show(HttpServletRequest request,Model model) throws Exception {
		int orderId = SuIntUtils.getInt(request.getParameter("orderId"));
		if(orderId != 0){
			OrderEntity orderEntity = orderService.getOrderBaseInfoById(orderId);
			model.addAttribute("orderEntity", orderEntity);	
		}
		
		return "/maintain/order/order_show";
	}
	
	@RequestMapping("to_edit_order")
	public String editOrder(HttpServletRequest request, Model model) throws Exception {
		int orderId = SuIntUtils.getInt(request.getParameter("orderId"));
		if(orderId != 0){
			OrderEntity orderEntity = orderService.getOrderBaseInfoById(orderId);
			model.addAttribute("orderEntity", orderEntity);
		}
		//业务员列表
		List<UserYwy> ywyList = ywyCache.getYwyList();
		model.addAttribute("ywyList", ywyList);
		
		return "/maintain/order/order_edit";
	}
	/**
	 * 保存订单信息
	 * @param orderDto
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="do_save_order",method=RequestMethod.POST)
	public String saveOrder(OrderDto orderDto, Model model) {
		try {
			orderService.saveOrder(orderDto ,model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/admin/order/index.action?" + orderDto.getDecodeQueryStr();
	}
	/**
	 * 保存订单并继续
	 * @param request
	 * @param model
	 * @param response
	 * @param orderDto
	 * @throws Exception
	 */
	@RequestMapping("do_save_order_and_goon")
	public void saveOrderAndGoon(HttpServletRequest request, Model model, HttpServletResponse response,OrderDto orderDto) throws Exception {
		PrintWriter writer = null;
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		writer = response.getWriter();
		String res = "suc";
		try {
			orderService.saveOrder(orderDto ,model);
		} catch (Exception e) {
			e.printStackTrace();
			res = e.getMessage();
		}
		writer.print(res);
		writer.flush();
	}
	
	@RequestMapping("destroy")
	public void destroy(HttpServletRequest request, Model model, HttpServletResponse response) throws Exception {
		PrintWriter writer = null;
		Map<String,String> msgs = new HashMap<String,String>();
		int orderId = SuIntUtils.getInt(request.getParameter("orderId"));
		if(orderId != 0){
			orderService.destroyOrder(orderId, msgs);
		}
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		writer = response.getWriter();
		writer.print(JSONObject.fromObject(msgs));
		writer.flush();
	}

}

